package com.lpf.back.controller;

import com.lpf.back.domain.Activity;
import com.lpf.back.domain.Admin;
import com.lpf.back.domain.User;
import com.lpf.back.service.impl.AdminServiceImp;
import com.lpf.back.service.interfaces.ActivityService;
import com.lpf.back.service.interfaces.AdminService;
import com.lpf.back.service.interfaces.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;

@Controller
@RequestMapping("/admin")
@EnableAutoConfiguration
public class AdminController {

    @Autowired
    private UserService userService;

//    @Autowired
//    private AdminService adminService;

    private final AdminService adminService;
    @Autowired
    public AdminController(AdminService adminService){
        this.adminService = adminService;
    }

    @Autowired
    private ActivityService activityService;

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String login(String username, String password, HttpSession session, Model model){
//        System.out.println(username + ", " + password);
//        AdminService adminService1 = new AdminServiceImp();
//        System.out.println(userService.login(username, password));
        int login = adminService.login(username, password);
        if(login == 1){
            session.setAttribute("username", username);
            //重定向到后台管理系统首页
            return "redirect:/index";
        }
        //登录不成功，返回登录页面中
        model.addAttribute("msg", "用户名或密码不正确！");
        return "login";
    }

//    @RequestMapping("/dynamic_release")
//    public int dynamicRelease(Activity activity){
//        return 0;
//    }
//
//    @RequestMapping("/user_management")
//    public String userManagement(Model model){
//        model.addAttribute(userService.getAllUsers());
//        return "user_management.html";
//    }
    @RequestMapping(value = "/setUserStatus", method = RequestMethod.GET)
    @ResponseBody
    public int setUserStatus(Integer id){
//        User user = userService.getUserById(id);
        System.out.println(id);
        return userService.updateStatus(id);
    }

    @RequestMapping(value = "/userManagement", method = RequestMethod.GET)
    public String userManagement(Model model){
        List<User> allUsers = userService.getAllUsers();
        for (User user : allUsers) {
            DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            user.setAvatar(format.format(user.getBeginData()));
            if(user.getStatus() == 1){
                user.setNickname("可用");
            } else if(user.getStatus() == 0){
                user.setNickname("不可用");
            }
        }
        model.addAttribute("users", allUsers);
        return "user_management";
    }

    @RequestMapping("/activityManagement")
    public String activityManagement(Model model){
        List<Activity> activities = activityService.getAllActivity();
        for (Activity activity : activities) {
            DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            activity.setTemp1(format.format(activity.getReleaseTime()));

            if(activity.getActiveStatus() == 1){
                activity.setTemp2("进行中");
            } else if (activity.getActiveStatus() == 0){
                activity.setTemp2("已结束");
            } else if (activity.getActiveStatus() == 2){
                activity.setTemp2("敬请期待");
            }
        }
        model.addAttribute("activities", activities);
        return "activity_management";
    }
}
